package com.itbaizhan.mapper;

import com.itbaizhan.pojo.User;
import com.itbaizhan.provider.UserProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

//开启二级缓存
@CacheNamespace(blocking = true)
public interface UserMapper {
    @Results(id = "userDiyMapper",value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username1"),
            @Result(property = "sex", column = "sex1"),
            @Result(property = "address", column = "address1")
    })
    @Select("select * from user")
    List<User> findAll();

    @ResultMap("userDiyMapper")
    @Select("select * from user where id = #{id}")
    User findById(int id);

    @SelectKey(keyColumn = "id",keyProperty = "id",resultType = int.class,
            before = false,statement = "SELECT LAST_INSERT_ID()")
    @Insert("insert into user(username,sex,address) values(#{username},#{sex},#{address})")
    void add(User user);

    @Update("update user set username = #{username},sex = #{sex},address = #{address} where id = #{id}")
    void update(User user);

    @Delete("delete from user where id = #{id}")
    void delete(int id);

    @Select("select * from user where username like #{username}")
    List<User> findByUsernameLike(String username);

    // 根据任意条件查询
//    @Select("<script>" +
//            "   select * from user\n" +
//            "       <where>\n" +
//            "           <if test=\"username != null and username.length() != 0\">\n" +
//            "              username like #{username}\n" +
//            "           </if>\n" + " <if test=\"sex != null and sex.length() != 0\">\n" +
//            "               and sex = #{sex}\n" +
//            "           </if>\n" +
//            "      <if test=\"address != null and address.length() != 0\">\n" +
//            "            and address = # {address}\n" +
//            "           </if>\n" +
//            "      </where>" +
//            "</script>")

    @SelectProvider(type = UserProvider.class,method = "findByConditionSql")
    List<User> findByCondition(User user);

}
